<?php
	class Image{
		
		var $i;
		
		function Image(){
			$this->i = NULL;
			header("Content-type: image/jpeg; image/gif");
		}
		
		function LoadImage($src){
			if(file_exists($src)){
				$this->i = imagecreatefromjpeg($src);
			}else
				$this->notFound();
			
		}
		
		function LoadThumb($src,$w,$h){
			if(file_exists($src)){
				//CARREGA A IMAGEM
				$imagem = @imagecreatefromjpeg($src);
				//PARAPARA RECIPIENTE
				$thumb = imagecreatetruecolor($w, $h);
				
				if($imagem){
					
					//PEGA IMAGEM CARREGADA E PROCESSA PARA TUMB COM A NOVA WITH E HEIGTH
					imagecopyresized($thumb, $imagem,0,0,0,0, $w, $h, $this->getWidth($src), $this->getHeight($src));
					$this->i = $thumb;
				}
			}else
				$this->notFound($w,$h);
		}
		
		function LoadwMark($src,$mark,$w,$h,$x,$y,$opacity=75){
			if(file_exists($src) && file_exists($mark)){
				//CARREGA A IMAGE
				$imagem_mark = imagecreatefromgif($mark);
				$imagem = imagecreatefromjpeg($src);
				//PARAPARA RECIPIENTE
				$thumb = imagecreatetruecolor($w, $h);
				//$thumb2 = imagecreatetruecolor(imagesx($imagem_mark), imagesy($imagem_mark)); 
				
				if($imagem){
					
					//PEGA IMAGEM CARREGADA E PROCESSA PARA TUMB COM A NOVA WITH E HEIGTH
					imagecopyresized($thumb, $imagem,0,0,0,0, $w, $h, $this->getWidth($src), $this->getHeight($src));
					
				//	imagecopyresized($thumb2, $imagem_mark,0,0,0,0, $this->getWidth($mark), $this->getWidth($mark), $this->getWidth($mark), $this->getHeight($mark));

					// COPIA A IMG MARK PRA CIMA DA THUMB E USA X_I E X_Y PARA O INICIO DO PONTO ATEH O _F
					//imagecopymerge($thumb,$thumb2,$x,$y,0,0,imagesx($thumb2),imagesy($thumb2),$opacity);
				    imagecopyresampled($thumb, $imagem_mark, $x, $y, 0, 0, imagesx($imagem_mark), imagesy($imagem_mark), imagesx($thumb), imagesy($thumb));

					$this->i = $thumb;
				}
			}else
				$this->notFound($w,$h);
			
		}
		
		
		function  LoadiString($src,$w,$h,$x,$y,$f,$string,$r,$g,$b){
			if(file_exists($src)){
				$imagem = @imagecreatefromjpeg($src);
				$thumb = imagecreatetruecolor($w, $h);
		
				
				imagecopyresized($thumb, $imagem,0,0,0,0, $w, $h, $this->getWidth($src), $this->getHeight($src));
				imagestring ($thumb, $f, 0, 0, $string, imagecolorallocate($thumb, $r,$g,$b));
				
				$this->i = $thumb;
			}else
				$this->notFound($w,$h);
		}
		
		function notFound($w=100,$h=100){
			
			$thumb = imagecreatetruecolor($w, $h);
			imagestring ($thumb, 3, $w/8, $h/4, urldecode('Foto'), imagecolorallocate($thumb,255,255,255));
			imagestring ($thumb, 3, $w/8, $h/2, urldecode('Indisponivel'), imagecolorallocate($thumb,255,255,255));
			
			$this->i = $thumb;
		}
		
		function Output($quality){
			if($this->i != NULL){
				imagejpeg($this->i);
				imagedestroy($this->i);
				$this->i = NULL;
			}
		}
		
		function getDimensions($src){
			if(file_exists($src)){
				return (list($width, $height, $type, $attr) = getimagesize($src));
			}
		}
		
		function getWidth($src){
			$array = $this->getDimensions($src);
			return $array[0];
		}
		
		function getHeight($src){
			$array = $this->getDimensions($src);
			return $array[1];
		}
	}
?>